//
//  PKDrawingToolsView.m
//  PDFKitTest
//
//  Created by Emo, on 2024/1/29.
//

#import "PKDrawingToolsView.h"

@interface PKDrawingToolsView () <PKDrawingColorOrLineDelegate>

/** 上一步（撤销）*/
@property (nonatomic, strong) UIButton * lastBtn;
/** 下一步（反撤销）*/
@property (nonatomic, strong) UIButton * nextBtn;
/** 颜色选择View*/
@property (nonatomic, strong) PKDrawingColorOrLineView * colorView;
/** 线粗细选择View*/
@property (nonatomic, strong) PKDrawingColorOrLineView * lineView;

@end

@implementation PKDrawingToolsView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    self.backgroundColor = HexAlphaColor(@"#000000", 0.4);
    
    [self addSubview:self.lastBtn];
    [self addSubview:self.nextBtn];
    [self addSubview:self.colorView];
    [self addSubview:self.lineView];
}


#pragma mark -- function --
- (void)setLastButtonSelected:(BOOL)selected {
    self.lastBtn.selected = selected;
    self.lastBtn.userInteractionEnabled = selected;
}

- (void)setNextButtonSelected:(BOOL)selected {
    self.nextBtn.selected = selected;
    self.nextBtn.userInteractionEnabled = selected;
}


#pragma mark -- click --
- (void)lastBtnClick:(UIButton *)sender {
    if (self.delegate && [self.delegate respondsToSelector:@selector(toolsView:didClickLastButton:)]) {
        [self.delegate toolsView:self didClickLastButton:sender];
    }
}

- (void)nextBtnClick:(UIButton *)sender {
    if (self.delegate && [self.delegate respondsToSelector:@selector(toolsView:didClickLastButton:)]) {
        [self.delegate toolsView:self didClickNextButton:sender];
    }
}


#pragma mark -- delegate --
- (void)colorOrLineWithType:(PKDrawingSelectType)type value:(NSString *)value {
    if (type == PKDrawingSelectTypeColor) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(toolsView:didClickColorButton:)]) {
            [self.delegate toolsView:self didClickColorButton:value];
        }
    } else if (type == PKDrawingSelectTypeLine) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(toolsView:didClickLineButton:)]) {
            [self.delegate toolsView:self didClickLineButton:[value integerValue]];
        }
    }
}




#pragma mark -- lazy --
- (UIButton *)lastBtn {
    if (!_lastBtn) {
        _lastBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        _lastBtn.frame = CGRectMake(15, (self.height-32)/2, 32, 32);
        [_lastBtn setImage:[UIImage imageNamed:@"lmr_laststepN"] forState:(UIControlStateNormal)];
        [_lastBtn setImage:[UIImage imageNamed:@"lmr_laststepN"] forState:(UIControlStateHighlighted)];
        [_lastBtn setImage:[UIImage imageNamed:@"lmr_laststepY"] forState:(UIControlStateSelected)];
        [_lastBtn addTarget:self action:@selector(lastBtnClick:) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _lastBtn;
}

- (UIButton *)nextBtn {
    if (!_nextBtn) {
        _nextBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        _nextBtn.frame = CGRectMake(CGRectGetMaxX(self.lastBtn.frame)+5, (self.height-32)/2, 32, 32);
        [_nextBtn setImage:[UIImage imageNamed:@"lmr_nextstepN"] forState:(UIControlStateNormal)];
        [_nextBtn setImage:[UIImage imageNamed:@"lmr_nextstepN"] forState:(UIControlStateHighlighted)];
        [_nextBtn setImage:[UIImage imageNamed:@"lmr_nextstepY"] forState:(UIControlStateSelected)];
        [_nextBtn addTarget:self action:@selector(nextBtnClick:) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _nextBtn;
}

- (PKDrawingColorOrLineView *)colorView {
    if (!_colorView) {
        _colorView = [[PKDrawingColorOrLineView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.nextBtn.frame), (self.height-60)/2, self.width-CGRectGetMaxX(self.nextBtn.frame)-90, 60) type:(PKDrawingSelectTypeColor)];
        _colorView.delegate = self;
    }
    return _colorView;
}

- (PKDrawingColorOrLineView *)lineView {
    if (!_lineView) {
        _lineView = [[PKDrawingColorOrLineView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.colorView.frame), (self.height-30)/2, 90, 30) type:(PKDrawingSelectTypeLine)];
        _lineView.delegate = self;
    }
    return _lineView;
}

@end
